package jihyun51.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JList;
import javax.swing.JFormattedTextField;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import javax.swing.AbstractListModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.TextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class BComp extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtbtn;
	private JPasswordField pwdtext2;
	private JList list_1;
	private JList list_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			BComp dialog = new BComp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public BComp() {
		setTitle(" :: 기본 컴퍼넌트 ::");
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JButton imgbtn = new JButton("이미지 버튼");
			imgbtn.setToolTipText("이미지버튼입니다.");
			imgbtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				}
			});
			imgbtn.setIcon(new ImageIcon(BComp.class.getResource("/images/icon/AIMP.png")));
			contentPanel.add(imgbtn);
		}
		{
			JLabel imglabel = new JLabel("이미지 레이블");
			imglabel.setIcon(new ImageIcon(BComp.class.getResource("/images/icon/icon2.png")));
			contentPanel.add(imglabel);
		}
		{
			JCheckBox chckbx1 = new JCheckBox("1");
			chckbx1.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					
					JCheckBox c=(JCheckBox)  e.getSource();
					if(c.isSelected())
						txtbtn.setText(c.getText());
					else
						txtbtn.setText("");
					
				}
			});
			contentPanel.add(chckbx1);
		}
		{
			JCheckBox chckbx2 = new JCheckBox("2");
			chckbx2.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JCheckBox c=(JCheckBox)  e.getSource();
					if(c.isSelected())
						txtbtn.setText(c.getText());
					else
						txtbtn.setText("");
				}
			});
			contentPanel.add(chckbx2);
		}
		{
			JCheckBox chckbx3 = new JCheckBox("3");
			chckbx3.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JCheckBox c=(JCheckBox)  e.getSource();
					if(c.isSelected())
						txtbtn.setText(c.getText());
					else
						txtbtn.setText("");
				}
			});
			
			contentPanel.add(chckbx3);
		}
		{
			//라디오 버튼 선언 시작
			JRadioButton rdbtn1 = new JRadioButton("1");
			rdbtn1.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JRadioButton r = (JRadioButton) e.getSource();
					if (r.isSelected())
						txtbtn.setText(r.getText());
					else
						txtbtn.setText("");
				}
			});
			contentPanel.add(rdbtn1);
	
			JRadioButton rdbtn2 = new JRadioButton("2");
			rdbtn2.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JRadioButton r = (JRadioButton) e.getSource();
					if (r.isSelected())
						txtbtn.setText(r.getText());
					else
						txtbtn.setText("");
				}
			});
			contentPanel.add(rdbtn2);

			JRadioButton rdbtn3 = new JRadioButton("3");
			rdbtn3.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JRadioButton r = (JRadioButton) e.getSource();
					if (r.isSelected())
						txtbtn.setText(r.getText());
					else
						txtbtn.setText("");
				}
			});
			contentPanel.add(rdbtn3);
			//라디오 버튼 선언 끝
			//라디오 버튼 그룹 생성 및 추가 
			ButtonGroup rbg=new ButtonGroup();
			rbg.add(rdbtn1);
			rbg.add(rdbtn2);
			rbg.add(rdbtn3);
		}
		{
			txtbtn = new JTextField();
			contentPanel.add(txtbtn);
			txtbtn.setColumns(10);
		}
		{
			JButton txtbtn1 = new JButton(" 입력문자 보기");
			txtbtn1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, txtbtn.getText());
					//textfield에 입력된 문자 출력
				}
			});
			contentPanel.add(txtbtn1);
		}
		{
			pwdtext2 = new JPasswordField();
			pwdtext2.setColumns(16);
			contentPanel.add(pwdtext2);
		}
		{
			JButton pwdbtn1 = new JButton(" 입력문자 보기 ");
			pwdbtn1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, pwdtext2.getPassword());
					//pwdtext2.getPassword()와 비교 해보자
	
				}
			});
			contentPanel.add(pwdbtn1);
		}
		{
			JScrollPane scrollPane1 = new JScrollPane();
			scrollPane1.setPreferredSize(new Dimension(100, 150));
			contentPanel.add(scrollPane1);
			{
				//JList에 추가할 항목을 배열로 생성
				String[] data={"1","2", "3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"};
				//생성된 배열을 JList의 생성자로 주입
				list_1 = new JList(data);
				scrollPane1.setViewportView(list_1);
			}
		}
		{
			JScrollPane scrollPane2 = new JScrollPane();
			scrollPane2.setPreferredSize(new Dimension(50, 150));
			contentPanel.add(scrollPane2);
			{
				list_2 = new JList();
				list_2.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						if(e.getClickCount()==2){
							JOptionPane.showMessageDialog(null, list_2.getSelectedIndex()+"/"+list_2.getSelectedValue());
						}
					}
				});
				list_2.setModel(new AbstractListModel() {
					String[] values = new String[] {"가", "나", "다", "라", "마", "바", "사", "아", "자", "차", "카", "타", "파", "하"};
					public int getSize() {
						return values.length;
					}
					public Object getElementAt(int index) {
						return values[index];
					}
				});
				scrollPane2.setViewportView(list_2);
			}
		}
		{
			JButton btnNewButton = new JButton(" 리스트선택보기 ");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, list_1.getSelectedIndex()+"/"+list_1.getSelectedValue());
				}
			});
			contentPanel.add(btnNewButton);
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane.setPreferredSize(new Dimension(150, 140));
			contentPanel.add(scrollPane);
			{
				TextArea textArea = new TextArea();
				textArea.setPreferredSize(new Dimension(150, 140));
				scrollPane.setViewportView(textArea);
			}
		}
		{
			String[] data={"1","2","3","4","5","6","7","8","9","10","11"};
			final JComboBox comboBox1 = new JComboBox(data);
			comboBox1.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JOptionPane.showMessageDialog(null, comboBox1.getSelectedItem()+"/"+comboBox1.getSelectedIndex());
				}
			});
			contentPanel.add(comboBox1);
		}
		{
			JComboBox comboBox2 = new JComboBox();
			comboBox2.setModel(new DefaultComboBoxModel(new String[] {"가", "나", "다", "라", "마", "바", "사", "아", "자"}));
			contentPanel.add(comboBox2);
		}
		//라디오 버튼 그룹 생성 및 추가 끝
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
